import "package:flutter/material.dart";
import 'package:dio/dio.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_swiper/flutter_swiper.dart';
import 'package:shop/service/service_method.dart';
import 'dart:convert';

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {

  int page = 1;
  List<Map> hotGoodsList=[];
  @override
  Widget build(BuildContext context) {
    var formData = {'lon':'115.02932','lat':'35.76189'};
    return Scaffold(
      backgroundColor: Color.fromRGBO(244, 245, 245, 1.0),
      appBar: AppBar(title: Text('百姓生活+'),),
      body: FutureBuilder(
        future: request('homePageContext',formData:formData),
        builder: (BuildContext context, AsyncSnapshot<dynamic> snapshot) {
          if(snapshot.hasData){
            var data=json.decode(snapshot.data.toString());
            List<Map> swiperDataList = (data['data']['slides'] as List).cast(); // 顶部轮播组件数
            List<Map> navigatorList =(data['data']['category'] as List).cast(); //类别列表
            String advertesPicture = data['data']['advertesPicture']['PICTURE_ADDRESS']; //广告图片
            String  leaderImage= data['data']['shopInfo']['leaderImage'];  //店长图片
            String  leaderPhone = data['data']['shopInfo']['leaderPhone']; //店长电话
            List<Map> recommendList = (data['data']['recommend'] as List).cast(); // 商品推荐
            String floor1Title =data['data']['floor1Pic']['PICTURE_ADDRESS'];//楼层1的标题图片
            String floor2Title =data['data']['floor2Pic']['PICTURE_ADDRESS'];//楼层1的标题图片
            String floor3Title =data['data']['floor3Pic']['PICTURE_ADDRESS'];//楼层1的标题图片
            List<Map> floor1 = (data['data']['floor1'] as List).cast(); //楼层1商品和图片
            List<Map> floor2 = (data['data']['floor2'] as List).cast(); //楼层1商品和图片
            List<Map> floor3 = (data['data']['floor3'] as List).cast(); //楼层1商品和图片
            return EasyRefresh(
                child: ListView(
                    children:<Widget>[
                      SwiperDiy(swiperDataList:swiperDataList ),   //页面顶部轮播组件
                      TopNavigator()
                    ]
                )
            );
          }else{
            return Center(
              child: Text('加载中'),
            );
          }
          return Text('');
        },
      ),
    );
  }
}
class SwiperDiy extends StatelessWidget{

  final List? swiperDataList;

  SwiperDiy({Key? key,this.swiperDataList}):super(key:key);

  @override
  Widget build(BuildContext context) {
    throw Container(
      color: Colors.white,
      height: ScreenUtil().setHeight(333),
      width: ScreenUtil().setWidth(750),
      child: Swiper(
        itemBuilder: (context,index){
          return InkWell(
            onTap: (){
              
            },
            child: Image.network("${swiperDataList?[index]['image']}",fit:BoxFit.fill),
          );
        },
        itemCount: swiperDataList?.length,
        pagination:new SwiperPagination(),
        autoplay: true,
      ),
    );
  }
}
class TopNavigator extends StatelessWidget {
  List? navigatorList;
  TopNavigator({Key? key,this.navigatorList}):super(key:key);

  Widget _gridViewItemUI(BuildContext context,item,index){
    return InkWell(
      onTap: (){

      },
      child: Column(
        children: <Widget>[
          Image.network(item['image'],width: ScreenUtil().setWidth(95)),
          Text(item['mallCategoryName'])
        ],
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    if(navigatorList!=null&&navigatorList!.length>10){
      navigatorList!.removeRange(10, navigatorList!.length);
    }
    var tempIndex=-1;
    return Container(
      color: Colors.white,
      margin: EdgeInsets.only(top: 5.0),
      height: ScreenUtil().setHeight(320),
      padding: EdgeInsets.all(3.0),
      child: GridView.count(
        physics: NeverScrollableScrollPhysics(),
        crossAxisCount: 5,
        padding:EdgeInsets.all(4.0),
        children: navigatorList!.map((item) {
          tempIndex++;
          return _gridViewItemUI(context, item,tempIndex);
        }
        ).toList(),
      ),
    );
  }
}

